﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UserModels;
using UserModels.DTO;
using UserRepositorys.Intface;

namespace UserRepositorys.Impl
{
    public class AccountRepository : Repository<AccountInfoModel>, IAccountRepository
    {
        private readonly IRoleRepository roleRepository;
        private readonly IAccountRoleRepository accountRoleRepository;
        public AccountRepository(ISqlSugarClient db, IRoleRepository roleRepository, IAccountRoleRepository accountRoleRepository) : base(db)
        {
            this.roleRepository = roleRepository;
            this.accountRoleRepository = accountRoleRepository;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="account"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> AddAccount(AccountInfoModel account)
        {
            var list= await base.GetAllasync();
            if(list.Where(x => x.AccountName == account.AccountName).Count() > 0)
            {
                return 0;
            }
           return await base.Addasync(account);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="accountRole"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> AddAccountRole(AccountRoleAddDTO accountRole)
        {
            var count = 0;
            AccountInfoModel model = new AccountInfoModel();
            model.AccountName = accountRole.AccountName;
            model.AccountPhone = accountRole.AccountPhone;
            model.AccountPass = accountRole.AccountPass;
            model.CreateTime = DateTime.Now;
           await AddAccount(model);
            var a = await base.GetAllasync();
            var b = a.Where(x => x.AccountName == accountRole.AccountName);
            var c = b.FirstOrDefault();
            var menuIds = accountRole.RoleId.Split(',');
            foreach (var id in menuIds)
            {
                AccountRoleModel roleModel= new AccountRoleModel();
                roleModel.AccountId = c.AccountId;
                roleModel.RoleId = Convert.ToInt32(id);
              var add=  await accountRoleRepository.Addasync(roleModel);
                count+=add;
            }
            return count;
        }

        /// <summary>
        /// 删除指定账户信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> DeleteAccount(int id)
        {
            return await base.Deleteasync(id);
        }

        /// <summary>
        /// 数据反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<AccountInfoModel> GetAccountInfo(int id)
        {
            return await base.GetModelasync(id);
        }

        /// <summary>
        /// 账户登录
        /// </summary>
        /// <param name="AccountPhone"></param>
        /// <param name="AccountPass"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<AccountInfoModel> GetAccountInfoById(string? AccountPhone, string? AccountPass)
        {
            var list = await base.GetAllAsync(x => x.AccountPhone == AccountPhone && x.AccountPass == AccountPass);
            if (list.Count > 0)
            {
                return list.FirstOrDefault();
            }
            return null;
        }

        /// <summary>
        /// 显示全部账户信息
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<List<AccountRoleDTO>> ShowAccount()
        {
            var urList = await accountRoleRepository.GetAccountRole();
            var roleList = await roleRepository.GetRoleAllList();
            var accountList = await base.GetAllasync();
            var roles = (from a in accountList
                         select new AccountRoleDTO
                         {
                             AccountId = a.AccountId,
                             AccountPhone = a.AccountPhone,
                             AccountName = a.AccountName,
                             RoleName = string.Join(',', (from r in roleList
                                                          join t in urList
                                                          on r.RoleId equals t.RoleId
                                                          where (a.AccountId == t.AccountId)
                                                          select r.RoleName
                                                         ).ToList()),
                             CreateTime = a.CreateTime
                         }
                       );
            return roles.ToList();
        }

        /// <summary>
        /// 账户数据修改
        /// </summary>
        /// <param name="accountInfo"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> UpdateAccount(AccountInfoModel accountInfo)
        {
           return await base.Updateasync(accountInfo);
        }
    }
}
